using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CSP.Model.Presenter;
using CSP.Model.Entities;
using System.Collections.Generic;

namespace CSP.WebControls
{
    public partial class WhatsOnYourMind : System.Web.UI.UserControl, IWhatsOnYourMindPresenter
    {
        private WhatsOnYourMindPresenter presenter = null;
        protected global::System.Web.UI.WebControls.Label lblTitle;
        protected global::System.Web.UI.WebControls.TextBox txtPeep;
        protected global::System.Web.UI.WebControls.ImageButton btnShare;
        protected global::System.Web.UI.WebControls.Button btnDelete;
        protected global::System.Web.UI.WebControls.Repeater rptPeeps;
        protected global::System.Web.UI.WebControls.Literal ltPeepValidator;
        protected global::System.Web.UI.HtmlControls.HtmlInputHidden hidtwitter;
        protected global::System.Web.UI.HtmlControls.HtmlInputHidden hidfacebook;
        protected global::System.Web.UI.WebControls.Image imgUser;
        
        #region Public Properties
        public string Title
        {
            get { return txtPeep.Text; }
            set { txtPeep.Text = value; }
        }
        public string User
        {
            get;
            set;
        }
        public string Password
        {
            get;
            set;
        }
        public DateTime Date
        {
            get { return DateTime.Now; }
        }
        public string SiteURL
        {
            get;
            set;
        }
        public string ListName
        {
            get;
            set;
        }
        public bool PostToTwitter
        {
            get
            {
                if (hidtwitter.Value.ToString() == string.Empty || hidtwitter.Value.ToString() == "Inactive")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
        public bool PostToFB
        {
            get
            {
                if (hidfacebook.Value.ToString() == string.Empty || hidfacebook.Value.ToString() == "Inactive")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
        public string UserImage
        {
            get { return imgUser.ImageUrl; }
            set { imgUser.ImageUrl = value; }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {               
                presenter = new WhatsOnYourMindPresenter(this);
                if (!Page.IsPostBack)
                {
                    presenter.Load();
                }
            }
            catch (Exception ex)
            {
                DisplayError(ex);
            }

        }
        protected void btnShare_OnClick(object sender, EventArgs e)
        {
            try
            {
                presenter.PostPeep();
                presenter.Load();

            }
            catch (Exception ex)
            {
                DisplayError(ex);
            }
        }
        protected void btnDelete_OnClick(object sender, EventArgs e)
        {
            try
            {
                presenter.DeleteAllPeeps();
                presenter.Load();

            }
            catch (Exception ex)
            {
                DisplayError(ex);
            }
        }
        public void BindUpdates(List<Peep> peeps)
        {
            try
            {
                rptPeeps.DataSource = peeps;
                rptPeeps.DataBind();
            }
            catch
            {
                throw;
            }
        }

        private void DisplayError(Exception ex)
        {
            this.Controls.Add(new LiteralControl("<div style='color:Red;display:none;'>" + ex.ToString() + "</div>"));
        }
        private bool IsPeepValid()
        {
            if (Title != null & Title.Trim() != string.Empty)
            {
                ltPeepValidator.Visible = false;
                return true;
            }
            return false;
        }
    }

}
